<template>
  <div>
    <el-row>
      <el-col :xs="24" :sm="0">
        <el-card>
          <el-date-picker v-model="value" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-card>
      </el-col>
    </el-row>
    <el-row :gutter="10">
      <el-col :xs="0" :sm="15" :md="14" :lg="13" :xl="12"
        ><el-card class="box-card" style="height: 100%">
          <el-date-picker v-model="value" type="date" placeholder="选择日期">
          </el-date-picker>
          <el-calendar v-model="value">
            <template slot="dateCell" slot-scope="{ date, data }">
              <p :class="data.isSelected ? 'is-selected' : ''">
                {{ data.day.split("-").slice(1).join("-") }}
                {{ data.isSelected ? "✔️" : "" }}
              </p>
            </template>
          </el-calendar>
        </el-card></el-col
      >
      <el-col :xs="24" :sm="9" :md="10" :lg="11" :xl="12"
        ><el-card class="box-card" v-loading="loading">
          <template v-if="isEdit">
            <el-input
              type="textarea"
              autosize
              placeholder="请输入内容"
              v-model="diary.diary"
              maxlength="255"
              show-word-limit
            >
            </el-input>
            <div style="margin-top: 10px">
              <el-button
                type="primary"
                size="mini"
                @click="addDiary(data)"
                :loading="loading"
                >确定</el-button
              >
            </div>
          </template>

          <template v-else>
            <div>
              <span>{{ diary.diary }}</span>
              <div style="margin-top: 10px">
                <el-row>
                  <el-col :span="19">
                    <el-button size="mini" @click="isEdit = true"
                      >编 辑</el-button
                    >
                  </el-col>
                  <el-col :span="5">
                    <el-button
                      type="primary"
                      size="mini"
                      @click="addPunchin"
                      :loading="loading"
                      >发布到打卡区</el-button
                    >
                  </el-col>
                </el-row>
              </div>
            </div>
          </template>
        </el-card></el-col
      >
    </el-row>
  </div>
</template>

<script>
import diaryApi from "@/api/diary";
import punchinApi from "@/api/punchin";

export default {
  data() {
    return {
      value: new Date(),
      diary: {},
      // 编辑标签
      isEdit: false,
      loading: false,
    };
  },
  methods: {
    getDiary(date) {
      this.loading = true;

      this.isEdit = false;
      const day = "" + date + "T00:00:00";
      const diary = {
        userId: this.$store.state.user.userId,
        createTime: day,
      };
      diaryApi.getDiaryByDate(diary).then((resp) => {
        this.diary = resp.data;
        if (this.diary == null) {
          this.diary = {
            diary: "这天每天写日记哦~",
            createTime: date + "T00:00:00",
            userId: this.$store.state.user.userId,
          };
        } else {
          this.diary.createTime = date + "T00:00:00";
        }
        this.loading = false;
      });
    },

    addDiary() {
      this.loading = true;
      diaryApi.addDiary(this.diary).then((resp) => {
        this.$message(resp.message);
        this.isEdit = false;
        this.loading = false;
      });
    },

    addPunchin() {
      this.loading = true;
      const data = {
        punchin: this.diary.diary,
        userId: this.$store.state.user.userId,
      };
      // 判断内容是否能发送
      if (
        data.punchin == "" ||
        data.punchin == "这天每天写日记哦~" ||
        data.punchin == null ||
        data.punchin == undefined
      ) {
        this.$message("请先编辑日记哦~");
        this.loading = false;
        return;
      }

      this.$confirm("确定要发布到打卡区吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          punchinApi.addpunchin(data).then((resp) => {
            // 显示消息提示
            this.$message({
              type: "success",
              message: resp.message,
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      this.loading = false;
    },
  },
  watch: {
    value(newVal, oldVal) {
      if (newVal !== null && newVal != undefined) {
        let year = newVal.getFullYear(); //年份
        let month = newVal.getMonth() + 1;
        let dates = newVal.getDate(); // 日

        let day =
          year +
          "-" +
          (month < 10 ? "0" + month : month) +
          "-" +
          (dates < 10 ? "0" + dates : dates);
        this.getDiary(day);
      }
      let year = oldVal.getFullYear(); //年份
      let month = oldVal.getMonth() + 1;
      let dates = oldVal.getDate(); // 日

      const day =
        year +
        "-" +
        (month < 10 ? "0" + month : month) +
        "-" +
        (dates < 10 ? "0" + dates : dates);
      this.getDiary(day);
    },
  },
  created() {
    const date = new Date();
    var year = date.getFullYear(); //年份
    var month = date.getMonth() + 1;
    var dates = date.getDate(); // 日

    const day =
      year +
      "-" +
      (month < 10 ? "0" + month : month) +
      "-" +
      (dates < 10 ? "0" + dates : dates);

    this.getDiary(day);
  },
};
</script>

<style scoped>
.el-card {
  margin: 10px;
}

.is-selected {
  color: #1989fa;
}
p {
  width: 100%;
  height: 100%;
}
</style>
